package com.android.kotlincoroutinemvvmdemo.hook

import java.lang.reflect.Field
import kotlin.jvm.Throws

object FieldUtil {

    @Throws(Exception::class)
    fun getField(clazz: Class<*>?, target: Any?, name: String): Any? {
        val field = clazz?.getDeclaredField(name)
        field?.isAccessible = true
        return field?.get(target)
    }

    @Throws(Exception::class)
    fun getField(clazz: Class<*>?, name: String): Field? {
        val field = clazz?.getDeclaredField(name)
        field?.isAccessible = true
        return field
    }

    @Throws(Exception::class)
    fun setField(clazz: Class<*>?, target: Any?, name: String, value: Any?) {
        val field = clazz?.getDeclaredField(name)
        field?.isAccessible = true
        field?.set(target, value)
    }
}